package br.com.drestranho.escola.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.com.drestranho.escola.entity.Chamada;
import br.com.drestranho.escola.entity.Disciplina;
import br.com.drestranho.escola.entity.Funcionario;
import br.com.drestranho.escola.entity.Matricula;




@Stateless
public class DisciplinaDAO {
	@PersistenceContext(unitName="H6Escola")
	private EntityManager em;
	
	public void salvar(Disciplina bean) {
	        em.persist(bean);
	}
	
	public void atualizar(Disciplina bean) {
		em.merge(bean);
	}
	
	public void remover( Disciplina bean) {
		em.remove(em.merge(bean));
	}
	
	public Disciplina getBean(Integer id) {
		Disciplina bean = em.find(Disciplina.class, id);
		return bean;
	}
	
	public List<Disciplina> getBeans(){//Lista
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Disciplina> cq = cb.createQuery(Disciplina.class);
        Root<Disciplina> from = cq.from(Disciplina.class);
        CriteriaQuery<Disciplina> select = cq.select(from);
        List<Disciplina> disciplinas = em.createQuery(select).getResultList();
        return disciplinas;
	}

   	public List<Disciplina> getDisciplinasPorProfessor(Funcionario funcionarioLogado) {
   		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Disciplina> cq = cb.createQuery(Disciplina.class);
		Root<Disciplina> from = cq.from(Disciplina.class);
		TypedQuery<Disciplina> tq = em.createQuery(cq.select(from).where(cb.equal(from.get("funcionario"), funcionarioLogado.getId())));
		return tq.getResultList();
	}

       
   	// Obtém um List de anos letivos distintos existentes no banco de dados
   	public List<Integer> carregaAnos() {
   		Query qry = em.createQuery("select distinct t.ano from Turma t");
   		ArrayList<Integer> anos = (ArrayList<Integer>)qry.getResultList();
   		return anos;
   	}

}   
